//
//  CZStatusDetailController.swift
//  GZWeibo666
//
//  Created by Apple on 15/12/1.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
import SVProgressHUD
import SVPullToRefresh

class CZStatusDetailController: UITableViewController {


    var status : CZStatus?

    var comments : [CZCommentContent]?
    
    let headerViewReuseIdentifier = "headerView"
    let commentCellReuseIdentifier = "commentCell"
    let noDataCellReuseIdentifier = "noDataCell"
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.registerClass(CZStatusNormalCell.self, forCellReuseIdentifier: CZStatusReuseIdentifier.NormalCell.rawValue)
        tableView.registerClass(CZStatusForwardCell.self, forCellReuseIdentifier: CZStatusReuseIdentifier.ForwardCell.rawValue)

//        let nib = UINib(nibName: "CZHeadView", bundle: nil)
//        tableView.registerNib(nib, forHeaderFooterViewReuseIdentifier: headerViewReuseIdentifier)
//        
//        prepareUI()
        let nib2 = UINib(nibName: "CZCommentCell", bundle: nil)
        tableView.registerNib(nib2, forCellReuseIdentifier: commentCellReuseIdentifier)
//        tableView.registerClass(CZCommentCell.self, forCellReuseIdentifier: commentCellReuseIdentifier)
        let nib1 = UINib(nibName: "CZNoDataCell", bundle: nil)
        tableView.registerNib(nib1, forCellReuseIdentifier: noDataCellReuseIdentifier)
        CZNetworkTool.sharedInstance.getCommentList(status?.id, count: 10) { (result, error) -> () in
            print("\(result)")
        }
        tableView.addInfiniteScrollingWithActionHandler { () -> Void in
            CZCommentContent.loadCommtnes(self.status?.id, count: 10, finished: { (modelArr) -> Void in
                self.comments = modelArr
                self.tableView.reloadData()
            })
        }

        
        bottomView.frame = (self.tabBarController!.tabBar.bounds)
        
        self.tabBarController?.tabBar.addSubview(bottomView)
        self.tabBarController?.tabBar.bringSubviewToFront(bottomView)
    }
    
    private func prepareUI(){

    }

    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return section  == 1 ? headerView : nil
    }
    
    override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 1{
            return 44
        }
        return 0
    }
    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 2
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        var section2row = comments == nil ? 1 : comments?.count
        
        return section == 0 ? 1 : section2row!
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        if indexPath.section == 0{
            let cell = tableView.dequeueReusableCellWithIdentifier(status!.cellId()) as! CZStatusCell
            cell.status = status
            
            cell.bgDidClick = { (cell : UITableViewCell) -> Void in
                let sdController = CZStatusDetailController()
                sdController.status = self.status!.retweeted_status
                self.navigationController?.pushViewController(sdController, animated: true)
            }
            
            // MARK: - 底部bottomView的 Block
            let buttonClick = { (action: CZComposeViewControllerType) -> ()  in
                print("转发")
                let ButtonVC = CZComposeViewController(controllerType: action)
                ButtonVC.status = self.status
                self.presentViewController(UINavigationController(rootViewController: ButtonVC), animated: true, completion: nil)
            }
            
            cell.bottomView.forwardBlock = buttonClick
            cell.bottomView.commentBlock = buttonClick
            cell.bottomView.likeBlock = {() ->Void in
                print("点赞")
                SVProgressHUD .showInfoWithStatus("功能暂未开放");
            }
            return cell
        }
        
        print("\(comments?.count)---------")
        if comments != nil{
            let commentCell = tableView.dequeueReusableCellWithIdentifier(commentCellReuseIdentifier) as! CZCommentCell
            commentCell.commentContent = comments?[indexPath.row]
            
            return commentCell
        }
        

        
        let noDataCell = tableView.dequeueReusableCellWithIdentifier(noDataCellReuseIdentifier)
        return noDataCell!

    }

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

        if status!.rowHeight != nil {

            return status!.rowHeight!
        }

        let cell = tableView.dequeueReusableCellWithIdentifier(status!.cellId()) as! CZStatusCell

        let rowHeight = cell.rowHeight(status!)

        status!.rowHeight = rowHeight
        
        return indexPath.section == 0 ? rowHeight : 44
    }
    

    
    lazy var headerView : CZHeadView = {
        var headerView = CZHeadView.headView()
        headerView.delegate = self
        return headerView
    }()
    

    lazy var bottomView: CZStatusBottomView = CZStatusBottomView()
   
}


extension CZStatusDetailController : CZheadViewDelegate{
    func CZheadViewBtnClick() {
        CZCommentContent.loadCommtnes(self.status?.id, count: 10, finished: { (modelArr) -> Void in
            self.comments = modelArr
            self.tableView.reloadSections(NSIndexSet(index: 1), withRowAnimation: UITableViewRowAnimation.Automatic)
        })
    }
}




//
//headView

protocol CZheadViewDelegate : NSObjectProtocol{
    func CZheadViewBtnClick()
}
class CZHeadView: UIView {

    weak var delegate : CZheadViewDelegate?
    
    @IBOutlet weak var countBtn: UIButton!
    
    var id : Int?
    
    
    @IBAction func BtnClick() {
        delegate?.CZheadViewBtnClick()
    }

    class func headView()->CZHeadView {
        return NSBundle.mainBundle().loadNibNamed("CZHeadView", owner: nil, options: nil).last as! CZHeadView
    }
}

